import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:google_fonts/google_fonts.dart';
import '../index.dart';

/// 举报类型
class ReportType {
  final String key;
  final String label;
  const ReportType(this.key, this.label);
}

const List<ReportType> kReportTypes = [
  ReportType('pornographicVulgarity', 'Pornographic/Vulgar'),
  ReportType('hateSpeech', 'Hate Speech'),
  ReportType('nauseating', 'Nauseating'),
  ReportType('illegal', 'Illegal/Violation'),
  ReportType('spam', 'Spam/Ads'),
  ReportType('other', 'Other'),
  ReportType('block', 'Block this user'),
];

class ReportDialog extends StatefulWidget {
  final String? title;
  final String? description;
  final void Function(ReportType type)? onSubmit;

  const ReportDialog({super.key, this.title, this.description, this.onSubmit});

  static Future<void> show(
    BuildContext context, {
    String? title,
    String? description,
    void Function(ReportType type)? onSubmit,
  }) {
    return showDialog(
      context: context,
      barrierDismissible: true,
      builder:
          (context) => ReportDialog(
            title: title,
            description: description,
            onSubmit: onSubmit,
          ),
    );
  }

  @override
  State<ReportDialog> createState() => _ReportDialogState();
}

class _ReportDialogState extends State<ReportDialog> {
  int? _selectedIndex;
  bool _submitting = false;

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: MyColors.dark,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16.r)),
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 24.h),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.title ?? 'Report',
              style: TextStyle(
                color: Colors.white,
                fontSize: 20.sp,
                fontFamily: GoogleFonts.mochiyPopOne().fontFamily,
                fontWeight: FontWeight.bold,
              ),
            ),
            if (widget.description != null) ...[
              SizedBox(height: 8.h),
              Text(
                widget.description!,
                style: TextStyle(
                  color: Colors.white.withAlpha(180),
                  fontSize: 14.sp,
                  fontFamily: GoogleFonts.inter().fontFamily,
                ),
              ),
            ],
            SizedBox(height: 20.h),
            ...List.generate(kReportTypes.length, (index) {
              final type = kReportTypes[index];
              final selected = _selectedIndex == index;
              return GestureDetector(
                onTap: () {
                  setState(() {
                    _selectedIndex = index;
                  });
                },
                child: Container(
                  margin: EdgeInsets.only(bottom: 12.h),
                  padding: EdgeInsets.symmetric(
                    horizontal: 12.w,
                    vertical: 12.h,
                  ),
                  decoration: BoxDecoration(
                    color:
                        selected
                            ? MyColors.blue.withOpacity(0.15)
                            : Colors.white.withOpacity(0.05),
                    borderRadius: BorderRadius.circular(10.r),
                    border: Border.all(
                      color: selected ? MyColors.blue : Colors.transparent,
                      width: 1.5,
                    ),
                  ),
                  child: Row(
                    children: [
                      Icon(
                        selected
                            ? Icons.radio_button_checked
                            : Icons.radio_button_unchecked,
                        color:
                            selected
                                ? MyColors.blue
                                : Colors.white.withAlpha(180),
                        size: 22.sp,
                      ),
                      SizedBox(width: 12.w),
                      Text(
                        type.label,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontFamily: GoogleFonts.inter().fontFamily,
                        ),
                      ),
                    ],
                  ),
                ),
              );
            }),
            SizedBox(height: 20.h),
            Row(
              children: [
                Expanded(
                  child: ElevatedButton(
                    onPressed:
                        _submitting || _selectedIndex == null
                            ? null
                            : () async {
                              setState(() => _submitting = true);
                              await Future.delayed(
                                const Duration(milliseconds: 300),
                              );
                              if (widget.onSubmit != null &&
                                  _selectedIndex != null) {
                                widget.onSubmit!(kReportTypes[_selectedIndex!]);
                              }
                              if (mounted) Navigator.of(context).pop();
                            },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: MyColors.blue,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10.r),
                      ),
                      padding: EdgeInsets.symmetric(vertical: 14.h),
                    ),
                    child:
                        _submitting
                            ? SizedBox(
                              width: 20.sp,
                              height: 20.sp,
                              child: CircularProgressIndicator(
                                strokeWidth: 2,
                                color: Colors.white,
                              ),
                            )
                            : Text(
                              'Report',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 16.sp,
                                fontFamily:
                                    GoogleFonts.mochiyPopOne().fontFamily,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
